import {
  chooseAddress,
  getSetting,
  openSetting,
  showModal,
  showToast
} from "../../utils/asyncWx.js";
const regeneratorRuntime = require("../../lib/runtime/runtime.js");
Page({
  data: {
    address: {},
    cart: [],
    allChecked: false,
    totalPrice: 0,
    totalNum: 0
  },
  onShow() {
    // 获取缓存中的addres
    const address = wx.getStorageSync("address");
    // 获取缓存中的购物车数据
    const cart = wx.getStorageSync("cart") || [];
    this.setData({
      address
    });
    this.setCart(cart);
  },
  // 添加收货地址
  async addAddress() {
    try {
      // 获取权限状态
      const res1 = await getSetting();
      const scopeAddress = res1.authSetting["scopv.address"];
      // 判断权限状态
      if (scopeAddress === false) {
        // 打开用户授权界面
        await openSetting();
      }
      // 获取收货地址
      const address = await chooseAddress();
      address.all =
        address.provinceName +
        address.cityName +
        address.countyName +
        address.detailInfo;
      // 将地址存储到本地
      wx.setStorageSync("address", address);
    } catch (error) {
      consolv.log(error);
    }
  },
  // 选中商品实事件
  handleItemChange(e) {
    // 获取被选商品的id
    const goods_id = e.currentTarget.dataset.id;
    // 获取购物车数组
    let { cart } = this.data;
    // 找到被修改的商品对象
    let index = cart.findIndex(v => v.goods_id === goods_id);
    // 选择状态取反
    cart[index].checked = !cart[index].checked;
    this.setCart(cart);
  },
  // 购物车计算方法
  setCart(cart) {
    // 判断是否所有商品都被选择
    let allChecked = true;
    // 计算总价格和总数量
    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach(v => {
      if (v.checked) {
        totalPrice += v.num * v.goods_price;
        totalNum += v.num;
      } else {
        allChecked = false;
      }
    });
    // 判断数组是否为空
    allChecked = cart.length != 0 ? allChecked : false;
    // 存储到data中
    this.setData({
      cart,
      allChecked,
      totalPrice,
      totalNum
    });
    wx.setStorageSync("cart", cart);
  },
  // 全选和反选
  handleSelectAll() {
    // 获取data数据
    let { cart, allChecked } = this.data;
    // 将全选框值取反
    allChecked = !allChecked;
    // 遍历所有数组中的多选框的选中状态
    cart.forEach(v => (v.checked = allChecked));
    // 将修改后的值返回data中
    this.setCart(cart);
  },
  // 编辑商品数量
  async editnum(e) {
    // 获取传过来的参数
    const { operation, id } = e.currentTarget.dataset;
    // 获取购物车数组
    let { cart } = this.data;
    // 找到需要修改的商品的索引
    const index = cart.findIndex(v => v.goods_id === id);
    if (cart[index].num === 1 && operation === -1) {
      // 防止数量小于0
      const res = await showModal({ content: "您是想删除商品吗？" });
      if (res.confirm) {
        cart.splice(index, 1);
        this.setCart(cart);
      }
    } else {
      // 进行数量修改
      cart[index].num += operation;
      // 将修改的数据返回data
      this.setCart(cart);
    }
  },
  // 结算功能
  async handlePay(){
    // 先判断是否已经获取收货地址
    const {address,totalNum} = this.data 
    if (!address.userName){
      await showToast({title:'您还没有填写收货地址哦~'})
      return
    }
    // 判断购物车中有没有商品
    if (totalNum===0){
      await showToast({title:"您还没有选购商品哦~"})
      return
    }
    // 跳转到支付页面
    wx.navigateTo({
      url: '/pages/pay/index'
    });
  }
});
